home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / arc42s.lbr / ARCPACK.MQC / arcpack.mac
Text File  |  1985-08-04  |  8KB  |  200 lines

  1. /*  ARC - Archive utility - ARCPACK
  2.  
  3. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  4. $define(version,Version $tag(
  5. TED_VERSION DB =3.08), created on $tag(
  6. TED_DATE DB =06/26/85) at $tag(
  7. TED_TIME DB =22:43:11))#
  8. $undefine(tag)#
  9.     $version
  10.  
  11. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  12.  
  13.     By:  Thom Henderson
  14.  
  15.     Description:
  16.          This file contains the routines used to compress a file
  17.          when placing it in an archive.
  18.  
  19.     Language:
  20.          Computer Innovations Optimizing C86
  21. */
  22. #include <stdio.h>
  23. #include "arc.h"
  24.  
  25. /* stuff for non-repeat packing */
  26.  
  27. #define DLE 0x90                       /* repeat sequence marker */
  28.  
  29. static unsigned char state;            /* current packing state */
  30.  
  31. /* non-repeat packing states */
  32.  
  33. #define NOHIST  0                      /* don't consider previous input*/
  34. #define SENTCHAR 1                     /* lastchar set, no lookahead yet */
  35. #define SENDNEWC 2                     /* run over, send new char next */
  36. #define SENDCNT 3                      /* newchar set, send count next */
  37.  
  38. /* packing results */
  39.  
  40. static long stdlen;                    /* length for standard packing */
  41. static int crcval;                     /* CRC check value */
  42.  
  43. pack(f,t,hdr)                          /* pack file into an archive */
  44. FILE *f, *t;                           /* source, destination */
  45. struct heads *hdr;                     /* pointer to header data */
  46. {
  47.     int c;                             /* one character of stream */
  48.     long ncrlen;                       /* length after packing */
  49.     long huflen;                       /* length after squeezing */
  50.     extern long lzwlen;                /* length after crunching */
  51.     long pred_sq(), file_sq();         /* stuff for squeezing */
  52.  
  53.     /* first pass - see which method is best */
  54.  
  55.     if(note)
  56.          printf(" analyzing, ");
  57.  
  58.     state = NOHIST;                    /* initialize ncr packing */
  59.     stdlen =  ncrlen = 0;              /* reset size counters */
  60.     crcval = 0;                        /* initialize CRC check value */
  61.  
  62.     init_cr();                         /* initialize for crunching */
  63.     init_sq();                         /* initialize for squeeze scan */
  64.  
  65.     while((c=getc_ncr(f))!=EOF)        /* for each byte of file */
  66.     {    ncrlen++;                     /* one more packed byte */
  67.          scan_sq(c);                   /* see what squeezing can do */
  68.          putc_cr(c,NULL);              /* see what crunching can do */
  69.     }
  70.     huflen = pred_sq();                /* finish up after squeezing */
  71.     fini_cr(NULL);                     /* finish up after crunching */
  72.  
  73.     /* standard set-ups common to all methods */
  74.  
  75.     fseek(f,0L,0);                     /* rewind input */
  76.     hdr->crc = crcval;                 /* note CRC check value */
  77.     hdr->length = stdlen;              /* set actual file length */
  78.     state = NOHIST;                    /* reinitialize ncr packing */
  79.  
  80.     /* choose and use the shortest method */
  81.  
  82.     if(stdlen<=ncrlen && stdlen<=huflen && stdlen<=lzwlen)
  83.     {    if(note)
  84.               printf("storing, ");     /* store without compression */
  85.          hdrver = 2;                   /* note packing method */
  86.          hdr->size = stdlen;           /* size does not change */
  87.          while((c=fgetc(f))!=EOF)      /* store it straight */
  88.               fputc(c,t);
  89.     }
  90.  
  91.     else if(ncrlen<huflen && ncrlen<lzwlen)
  92.     {    if(note)
  93.               printf("packing, ");     /* pack with repeat suppression */
  94.          hdrver = 3;                   /* note packing method */
  95.          hdr->size = ncrlen;           /* set data length */
  96.          while((c=getc_ncr(f))!=EOF)
  97.               fputc(c,t);
  98.     }
  99.  
  100.     else if(huflen<lzwlen)
  101.     {    if(note)
  102.               printf("squeezing, ");
  103.          hdrver = 4;                   /* note packing method */
  104.          hdr->size = file_sq(f,t);     /* note final size */
  105.     }
  106.  
  107.     else
  108.     {    if(note)
  109.               printf("crunching, ");
  110.          hdrver = 6;
  111.          hdr->size = lzwlen;           /* size should not change */
  112.          init_cr();
  113.          while((c=getc_ncr(f))!=EOF)
  114.               putc_cr(c,t);
  115.          fini_cr(t);
  116.     }
  117.  
  118.     /* standard cleanups common to all methods */
  119.  
  120.     if(note)
  121.          printf("done.\n");
  122. }
  123.  
  124. /*  Non-repeat compression - text is passed through normally, except that
  125.     a run of more than two is encoded as:
  126.  
  127.          <char> <DLE> <count>
  128.  
  129.     Special case: a count of zero indicates that the DLE is really a DLE,
  130.     not a repeat marker.
  131. */
  132.  
  133. int getc_ncr(f)                        /* get bytes with collapsed runs */
  134. FILE *f;                               /* file to get from */
  135. {
  136.     static int lastc;                  /* value returned on last call */
  137.     static int repcnt;                 /* repetition counter */
  138.     static int c;                      /* latest value seen */
  139.  
  140.     switch(state)                      /* depends on our state */
  141.     {
  142.     case NOHIST:                       /* no relevant history */
  143.          state = SENTCHAR;
  144.          return lastc = getch(f);      /* remember the value next time */
  145.  
  146.     case SENTCHAR:                     /* char was sent. look ahead */
  147.          switch(lastc)                 /* action depends on char */
  148.          {
  149.          case DLE:                     /* if we sent a real DLE */
  150.               state = NOHIST;          /* then start over again */
  151.               return 0;                /* but note that the DLE was real */
  152.  
  153.          case EOF:                     /* EOF is always a special case */
  154.               return EOF;
  155.  
  156.          default:                      /* else test for a repeat */
  157.               for(repcnt=1; (c=getch(f))==lastc && repcnt<255; repcnt++)
  158.                    ;                   /* find end of run */
  159.  
  160.               switch(repcnt)           /* action depends on run size */
  161.               {
  162.               case 1:                  /* not a repeat */
  163.                    return lastc = c;   /* but remember value next time */
  164.  
  165.               case 2:                  /* a repeat, but too short */
  166.                    state = SENDNEWC;   /* send the second one next time */
  167.                    return lastc;
  168.  
  169.               default:                 /* a run - compress it */
  170.                    state = SENDCNT;    /* send repeat count next time */
  171.                    return DLE;         /* send repeat marker this time */
  172.               }
  173.          }
  174.  
  175.     case SENDNEWC:                     /* send second char of short run */
  176.          state = SENTCHAR;
  177.          return lastc = c;
  178.  
  179.     case SENDCNT:                      /* sent DLE, now send count */
  180.          state = SENDNEWC;
  181.          return repcnt;
  182.  
  183.     default:
  184.          abort("Bug - bad ncr state\n");
  185.     }
  186. }
  187.  
  188. static int getch(f)                    /* special get char for packing */
  189. FILE *f;                               /* file to get from */
  190. {
  191.     int c;                             /* a char from the file */
  192.  
  193.     if((c=fgetc(f))!=EOF)              /* if not the end of file */
  194.     {    crcval = addcrc(crcval,c);    /* then update CRC check value */
  195.          stdlen++;                     /* and bump length counter */
  196.     }
  197.  
  198.     return c;
  199. }
  200.